package com.vcms.controller.customer.states;

import com.vcms.controller.customer.TransactionController;
import com.vcms.controller.customer.ChangeGiver;
import com.vcms.controller.customer.DispenseController;
import com.vcms.controller.customer.CoinReceiver;
import com.vcms.controller.storemanagement.StoreController;
import com.vcms.entity.store.DrinksStore;
import com.vcms.entity.store.item.DrinksStoreItem;
import com.vcms.entity.store.itemtype.DrinksBrand;
import com.vcms.exception.VMCSException;


public class ReadyState extends TransactionState {

	public ReadyState(TransactionController transactionController){
		setTransactionController(transactionController);
	}
	
	@Override
	public boolean handleDisplayCustomerPanel() {
		System.out.println("[ReadyState] - Incorrect state to handle display customer panel!");
		
		return false;
	}
	
	@Override
	public boolean handleStartTransaction() {
		System.out.println("Inside Ready State Handle - Select Drinks Brand");
		
		try {
			TransactionController transactionController = getTransactionController();
			
			DrinksStore drinksStore = (DrinksStore) transactionController.getMainControler().getStoreController().getStore(StoreController.DRINK);
			DrinksStoreItem drinksStoreItem = (DrinksStoreItem) drinksStore.getStoreItem(transactionController.getSelection());
			DrinksBrand drinksBrand = (DrinksBrand)drinksStoreItem.getContent();

			transactionController.setPrice(drinksBrand.getPrice());
			
			ChangeGiver changeGiver = transactionController.getChangeGiver();
			DispenseController dispenseController = transactionController.getDispenseController();
			CoinReceiver coinReceiver = transactionController.getCoinReceiver();
			
			changeGiver.resetChange();
			dispenseController.resetCan();
			changeGiver.displayChangeStatus();
			dispenseController.allowSelection(false);
			coinReceiver.startReceive();
			
			// Transit to COININPUT state
			transactionController.transitState(StateFactory.getInstance().create(TransactionState.COININPUT, transactionController));
		} catch (VMCSException e) {
			e.printStackTrace();
		}
				
		return true;
	}
	
	@Override
	public boolean handleProcessMoneyReceived() {
		System.out.println("[ReadyState] - Incorrect state to handle process money received!");
		
		return false;
	}
	
	@Override
	public boolean handleCompleteTransaction() {
		System.out.println("[ReadyState] - Incorrect state to complete transaction!");
		
		return false;
	}
	
	@Override
	public boolean handleTerminateTransaction() {
		System.out.println("[ReadyState] - Incorrect state to terminate transaction!");
		
		return false;
	}
	
	@Override
	public boolean handleTerminateFault() {
		System.out.println("[ReadyState] - Incorrect state to handle terminate fault!");
		
		return false;
	}
	
	@Override
	public boolean handleRefreshCustomerPanel() {
		System.out.println("[ReadyState] - Incorrect state to handle refresh customer panel!");
		
		return false;
	}
	
	public void printState(){
		System.out.println("- Ready State -");
	}

}
